package com.motus.pm.action;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.motus.pm.service.EmpService;
import com.motus.pm.vo.MotusEmpInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
public class ForwardAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String name;

	@Autowired
	@Qualifier("empServiceImpl")
	private EmpService empService;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	@Override
	public String execute() throws Exception {
		// 定义返回值
		String type;
		if (name == null) {
			return "error";
		} else {
			MotusEmpInfo empInfo = empService.getEmpInfoByName(name);
			int userId = empInfo.getId();
			int deptId = empService.getDeptIdByUserId(userId);
			ActionContext ac = ActionContext.getContext();
			Map<String, Object> session = ac.getSession();
			session.put("userId", userId);
			session.put("deptId", deptId);
			session.put("name", name);

			// 判断权限
			int isAdmin = 0;
			int isBoss = 0;
			isAdmin = empInfo.getIsAdmin();
			isBoss = empInfo.getIsBoss();
			if (isAdmin == 0 && isBoss == 0) {
				type = "emp";
				session.put("auth", "emp");
			} else {
				if (isAdmin == 1 && isBoss == 0) {
					if (deptId == 5) {
						type = "projectPlan";
					} else {
						type = "dept";
						session.put("auth", "admin");
					}
				} else {
					type = "company";
					session.put("auth", "company");
				}
			}
		}
		return type;
	}
}
